/***
 * 商品表的数据模型
 * @type {Goods}
 ***/
/****************/
module.exports = class Goods extends require('./model') {

  /**
   * 添加商品至数据库
   * @param goods
   * @returns {Promise<unknown>}
   */
  static setGoods(goods) {
    return new Promise((resolve, reject) => {
      let sql = 'INSERT INTO goods SET ?';

      console.log('添加商品sql='+sql);
      console.log('name=' + goods.name + ';describe=' + goods.describe + ';price=' + goods.price + ';store=' +
        goods.store + ';src=' + goods.src + ';goods_category_id=' + goods.goods_category_id + ';index=' + goods.index + ';--------------------------');

      this.dbQuery(sql , goods )
        .then(results => {
          resolve(results.insertId);  //插入成功，则返回插入成功数据的id
        })
        .catch(error => {
          console.log(`添加商品失败，${error.message}`)
          reject(error);
        })
    })
  }

  /**
   * 删除商品
   * @param id 商品id
   * @returns {Promise<unknown>}
   */
  static deleteGoods(id) {
    return new Promise((resolve, reject) => {
      let sql = 'DELETE FROM goods WHERE id = ?'

      console.log('删除商品sql=' + sql);
      console.log('id=' + id + ';---------------------------------------------');

      this.dbQuery(sql, id)
        .then(result => {
          resolve(result.affectedRows); // 删除成功返回受影响行数
        })
        .catch(error => {
          console.log('删除商品' + id + '失败' + error);
          reject(error);
        })
    })
  }

  /**
   * 更新商品信息
   * @param goods
   * @returns {Promise<更新商品信息>}
   */
  static upDateGoods(goods) {
    return new Promise((resolve, reject) => {
      let sql = `UPDATE goods SET goods.name = '${goods.name}', goods.describe = '${goods.describe}', goods.price = ${goods.price}, goods.store = ${goods.store}, 
       goods.src = '${goods.src}', goods.goods_category_id = ${goods.goods_category_id}, goods.index = ${goods.index} WHERE goods.id = ${goods.id}`

      console.log('更新商品信息sql=' + sql);
      console.log('name=' + goods.name + ';describe=' + goods.describe + ';price=' + goods.price + ';src='
        + goods.src + ';goods_category_id=' + goods.goods_category_id + ';index=' + goods.index + ';id=' + goods.id);

      this.dbQuery(sql)
        .then(result => {
          resolve(result.affectedRows);
        })
        .catch(error => {
          reject(error);
        })
    })
  }

  /**
   * 获取商品的数量
   * @param id 商品id
   * @param goods_category_id 商品类目id
   * @param root_category_id 商品所属的root的id
   * @param username 商家用户名
   * @param name_describe_goodsCategoryName_rootCategoryName 商品名字、描述,或者商品的商品类目名字、root类目名
   * @returns {Promise<获取商品的数量>}
   */
  static getGoodsCount(id, goods_category_id, root_category_id, username, name_describe_goodsCategoryName_rootCategoryName) {
    return new Promise((resolve, reject) => {
      let sql = `SELECT COUNT(1) AS count 
        FROM goods, goods_category, root_category, user 
        WHERE (1 = 1 AND goods_category.id = goods.goods_category_id AND goods_category.root_category_id = root_category.id 
         AND user.username = goods_category.user_username) `

      sql += id != 0 && id ? ` AND goods.id = '${id} ' ` : ' '
      sql += goods_category_id != 0 && goods_category_id ? ` AND goods.goods_category_id = ${goods_category_id} ` : ' '
      sql += root_category_id != 0 && root_category_id ? ` AND root_category.id = ${root_category_id} ` : ' '
      sql += username != '' && username ? ` AND goods_category.user_username = '${username}' ` : ' '

      sql += name_describe_goodsCategoryName_rootCategoryName != '' && name_describe_goodsCategoryName_rootCategoryName ?
        ` AND (goods.name LIKE '%${name_describe_goodsCategoryName_rootCategoryName}%' OR 
          goods.describe LIKE '%${name_describe_goodsCategoryName_rootCategoryName}%' OR 
          goods_category.name LIKE '%${name_describe_goodsCategoryName_rootCategoryName}%' OR 
          root_category.name LIKE '%${name_describe_goodsCategoryName_rootCategoryName}%') ` : ' '

      console.log('商品数量sql=' + sql);
      console.log('id=' + id+ ';goods_category_id=' + goods_category_id + ';root_category_id=' + root_category_id + ';username=' + username +
        ';name_describe_goodsCategoryName_username_rootCategoryName=' + name_describe_goodsCategoryName_rootCategoryName +
        ';-------------------------------------------------------');

      this.dbQuery(sql)
        .then(result => {
          resolve(result[0]);
        })
        .catch(error => {
          reject(error);
        })
    })
  }

  /**
   * 获取指定的商品信息
   * @param start
   * @param size
   * @param id 商品id
   * @param goods_category_id 商品类目id
   * @param root_category_id 商品所属的root的id
   * @param username 商家用户名
   * @param name_describe_goodsCategoryName_rootCategoryName 商品名字、描述,或者商品的商品类目名字、root类目名
   * @returns {Promise<获取指定的商品信息>}
   */
  static getGoods(start, size, id, goods_category_id, root_category_id, username, name_describe_goodsCategoryName_rootCategoryName) {
    return new Promise((resolve, reject) => {
      let sql = `SELECT goods.*, goods_category.name goods_category_name, goods_category.user_username, 
        root_category.id root_category_id, root_category.name root_category_name, 
        user.address 
        FROM goods, goods_category, root_category, user 
        WHERE (1 = 1 AND goods_category.id = goods.goods_category_id AND goods_category.root_category_id = root_category.id 
         AND user.username = goods_category.user_username) `

      sql += id != 0 && id ? ` AND goods.id = '${id} ' ` : ' '
      sql += goods_category_id != 0 && goods_category_id ? ` AND goods.goods_category_id = ${goods_category_id} ` : ' '
      sql += root_category_id != 0 && root_category_id ? ` AND root_category.id = ${root_category_id} ` : ' '
      sql += username != '' && username ? ` AND goods_category.user_username = ${username} ` : ' '

      sql += name_describe_goodsCategoryName_rootCategoryName != '' && name_describe_goodsCategoryName_rootCategoryName ?
        ` AND (goods.name LIKE '%${name_describe_goodsCategoryName_rootCategoryName}%' OR 
          goods.describe LIKE '%${name_describe_goodsCategoryName_rootCategoryName}%' OR 
          goods_category.name LIKE '%${name_describe_goodsCategoryName_rootCategoryName}%' OR 
          root_category.name LIKE '%${name_describe_goodsCategoryName_rootCategoryName}%') ` : ' '

      sql += ` ORDER BY goods.index DESC LIMIT ?, ? `

      console.log('获取商品信息sql' + sql);
      console.log('id=' + id+ ';goods_category_id=' + goods_category_id + ';root_category_id=' + root_category_id + ';username=' + username +
        ';name_describe_goodsCategoryName_username_rootCategoryName=' + name_describe_goodsCategoryName_rootCategoryName +
        ';start=' + start + ';size=' + size + ';--------------------------------------')

      this.dbQuery(sql, [start, size])
        .then(result => {
          resolve(result)
        })
        .catch(error => {
          reject(error)
        })
    })
  }



}





